package tt.dz.service.imp.base;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tt.dz.dao.base.OmsCommonDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.base.OmsPubParam;
import tt.dz.entity.exception.OmsDaoException;
import tt.dz.service.base.PubParamService;
import tt.dz.util.OmsConstant.MsgCodeEnum;
@Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class PubParamServiceImpl extends BaseService implements PubParamService{
	@Autowired
	@Qualifier("base")
	private OmsCommonDao<OmsPubParam> omsCommonDaoImpl;
	
	@Override
	public  synchronized JsonCommon<?> add(OmsPubParam pubParam) 
	{
		try 
		{
			String maxId = omsCommonDaoImpl.findMaxId(OmsPubParam.class);
			maxId = genarateId(maxId,0);
			if (StringUtils.isNotEmpty(maxId))
			{
				pubParam.setId(maxId);
				omsCommonDaoImpl.add(pubParam);
				return this.buildSuccessJsonCommon(pubParam);
			}
		} catch (OmsDaoException e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
		return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:genarate id fail");
	}

	@Override
	public JsonCommon<?> update(OmsPubParam pubParam) 
	{
		if (StringUtils.isEmpty(pubParam.getId()))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try {
			omsCommonDaoImpl.update(pubParam);
			return this.buildSuccessJsonCommon(pubParam);
		} catch (OmsDaoException e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> delete(String id) {
		if (StringUtils.isEmpty(id))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try {
			omsCommonDaoImpl.deleteById(id, OmsPubParam.class);
			return this.buildSuccessJsonCommon("success");
		} catch (OmsDaoException e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> findById(String id) {
		if (StringUtils.isEmpty(id))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try 
		{
			OmsPubParam pubParam = new OmsPubParam();
			pubParam.setId(id);
			OmsPubParam op = omsCommonDaoImpl.findRecordByPK(pubParam);
			return this.buildSuccessJsonCommon(op);
		} 
		catch (Exception e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonForList<?> findPageObjs(OmsCommonParam param, OmsPubParam pubParam) 
	{
		try 
		{
			List<OmsPubParam> lst = omsCommonDaoImpl.pagingFind(param, pubParam);
			int total = 0;
			if (param.isQueryTotal())
			{
				total = omsCommonDaoImpl.queryTotalByObject(pubParam);
			}
			return this.buildSuccessJsonForList(param, lst, total);
		} catch (Exception e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonForList();
		}
	}

}
